<?php
namespace Yd\Controller;
use Common\Controller\HomebaseController;

class YdController extends HomebaseController{
	
	protected $reservation_model;
	function _initialize() {
		parent::_initialize();
		$this->reservation_model =D("Common/Reservation");
	}
	
	/*预订*/
	function index(){
		//var_dump(sp_get_current_user());
		//$r = $this->reservation_model->get_arr_yd('2016-7-16');
		//echo "预订列表：";
		//var_dump($r);
		//echo "<br /> 时间列表：";
		//$r = $this->reservation_model->get_arr_time();
		//var_dump($r);
		//echo "<br /> 场地列表：";
		//$r = $this->reservation_model->get_arr_cd(13);	//13羽毛球馆
		//var_dump($r);
		//echo "<br /> 生成表格：";
		//header("Content-type: text/html; charset=utf-8");
		$cgid = 13;
		if(IS_GET){
			$cgid = I('get.cgid',13);
		}
		$cdname = $this->reservation_model->get_cd_name($cgid);		//取出场地名称
		//echo "cdname:" . $cdname;
		//$_date = I('get.date',date('y-m-d'));
		$birthday = date("Y-m-d");
		//$table = $this->reservation_model->get_table($_date,$cgid);
		//echo $table;
		//echo $this->ret_limitdate();
		$msg2 = $this->reservation_model->get_msg2();
		
		$user_type = get_current_usertype();	//1表示管理员，2表示普通用户
		
		$this->assign("birthday",$birthday);	//输出当前日期
		$this->assign("cdname", $cdname);	//场地名称
		//$this->assign("table", $table);		//生成表格框架
		$this->assign("msg2",$msg2);	//输出预订的消息内容，在网页里还有定时器每3秒刷新消息
		$this->assign("input_tel",$this->reservation_model->get_input_tel());	//根据系统设置及用户类型，输出电话号码输入框
		$this->assign("input_yd_xm",$this->reservation_model->get_input_xm());	//管理员可以更改预订人的显示名称
		$this->assign("ydstatus",$this->ret_ydstatus());			//显示当前的预订限制和规则
		$this->assign("yddatelimit",$this->ret_limitdate());		//当前预订的限制日期
		
		//不使用列表方式显示日期选择$this->assign("nav_date",$nav_date);
		switch ($user_type){
			case 1:	//管理员
				$table = $this->reservation_model->get_table($birthday,$cgid);
				$this->assign("table", $table);		//生成表格框架
				$this->display("admin");
				break;
			default:	//其它普通用户
				$table = $this->reservation_model->get_table($birthday,$cgid);
				$this->assign("table", $table);		//生成表格框架
				$this->display("index");
				break;
		}
	}
	
	/*AJAX 预订的POST*/
	function ajax_yd(){
		if (IS_POST) {
			$data = I('post.arr');
			//echo json_encode($data);
			echo json_encode($this->reservation_model->model_ajax_yd($data));
		}
	}
	
	/*动态刷新预订表*/
	function ajax_refresh(){
		if(IS_POST){
			$data = I('post.refresh');
			//返回选择日期的预订表数据
			//echo $this->reservation_model->model_ajax_refresh($data);
			echo json_encode($this->reservation_model->model_ajax_refresh($data));
		}
	}
	
	/*AJAX的消息传送*/
	function ajax_msg2(){
		echo $this->reservation_model->get_msg2();
	}
	
	/*jquery取用户的类型1表示管理员，2表示普通用户*/
	function ajax_get_usertype(){
		echo get_current_usertype();
	}
	
	/*取消普通用户的预订 */
	function ajax_cancel(){
		if(!sp_check_verify_code()){
			$this->error("验证码错误！");
		}
		if (IS_POST) {
			$admin_id = sp_get_current_userid();
			$cancel_reason = I('post.cancel_reason'); 
			$cancel_place_id = I('post.cancel_placeId'); 
			$cancel_opentime_id = I('post.cancel_opentimeId');
			$cancel_riqi = I('post.cancel_riqi'); 
			//$this->success("reason:" . $cancel_reason . "；place_id:" . $cancel_place_id . "；opentime_id:" . $cancel_opentime_id . "；riqi:" . $cancel_riqi); 
			$result = $this->reservation_model->cancel_yd($cancel_reason,$cancel_place_id,$cancel_opentime_id,$cancel_riqi,$admin_id);
			$to=empty($_SESSION['Cancel_Yd'])?"Yd/Yd":$_SESSION['Cancel_Yd'];
			if ($result > 0) {
				$this->success("取消用户预订成功！");
			} else {
				$this->error("取消用户预订失败！");
			}
		}
	}
	
	//读取当前预订的日期限制、预订的场地限制等，返回字符串
	function ret_ydstatus(){
		$m = M();
		$sqlstr = "select fun_ret_ydstatus() as ydstatus";
		$result = $m->query($sqlstr);
		return $result[0]["ydstatus"];
	}
	
	//取得预订的截止日期，管理员的截止日期到2020-9-9
	function ret_limitdate(){
		$user_id = sp_get_current_userid();	//取得已登陆用户的ID号
		//$user_type = $this->reservation_model->chk_admin($user_id);
		$user_type = get_current_usertype();	//取得用户是否为管理员，1表示管理员，2表示普通用户
		if($user_type == 1){
			return "2020-9-9";
		}
		$m = new \Yd\Model\YddatelimitModel();
		$where['id'] = 1;
		$result = $m->where($where)->getField('end-date');
		return $result;
	}
	
}
